package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.manager.service.ICircleFollowService;
import com.codedancer.campus.manager.service.bo.circle.CircleFollowPageSearchBo;
import com.codedancer.campus.manager.service.vo.circle.CircleFollowPageVo;
import com.codedancer.campus.mapper.CircleFollowMapper;
import com.codedancer.campus.mapper.UserMapper;
import com.codedancer.campus.po.CircleFollow;
import com.codedancer.campus.po.User;
import com.codedancer.campus.qo.circle.CircleFollowPageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class CircleFollowServiceImpl implements ICircleFollowService {

    @Autowired
    private CircleFollowMapper circleFollowMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询某个圈子关注用户列表
     *
     * @param circleFollowPageSearchBo
     * @return
     */
    @Override
    public IPage<CircleFollowPageVo> page(CircleFollowPageSearchBo circleFollowPageSearchBo) {
        CircleFollowPageSearchQo circleFollowPageSearchQo = BeanUtil
            .copyProperties(circleFollowPageSearchBo, CircleFollowPageSearchQo.class);
        IPage<CircleFollow> circleFollowIPage = circleFollowMapper.page(
            new Page<CircleFollow>(circleFollowPageSearchBo.getPage(),
                circleFollowPageSearchBo.getSize()), circleFollowPageSearchQo);
        List<CircleFollowPageVo> circleFollowPageVos;
        IPage<CircleFollowPageVo> circleFollowPageVoIPage = new Page<>();
        circleFollowPageVos = circleFollowIPage.getRecords().stream().map(circleFollow -> {
            CircleFollowPageVo circleFollowPageVo = BeanUtil
                .copyProperties(circleFollow, CircleFollowPageVo.class);
            User user = userMapper.selectById(circleFollow.getFollowUserUuid());
            circleFollowPageVo.setFollowUserAvatar(user.getAvatar());
            circleFollowPageVo.setFollowUserNickName(user.getNickName());
            circleFollowPageVo.setFollowTime(circleFollow.getCreateTime());
            return circleFollowPageVo;
        }).collect(Collectors.toList());
        circleFollowPageVoIPage.setRecords(circleFollowPageVos);
        circleFollowPageVoIPage.setTotal(circleFollowIPage.getTotal());
        return circleFollowPageVoIPage;
    }
}
